package com.dudu.dao;

import java.sql.Connection;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.dudu.model.UploadFileModel;

import core.CoreDao;
import core.db.DBConnectionUtil;
/**
 * 上传文件处理DAO
 * @author gaoxs
 *
 */
public class UploadFileDao {
	private static final Logger log = Logger.getLogger(UploadFileDao.class.getSimpleName());
	/**
	 * 
	 * 新增上传文件信息
	 * @param coreDao
	 * @return
	 */
	public int insertFileInfo(CoreDao coreDao, String app_key,String cust_account,String fileAddress,String mediaName,String voice_name,String content,String fileFullName) {
		// 数据库连接
		Connection con = null;
		// 最终返回结果
		int result = -1;
		try {
			// 获取数据库连接
			con = DBConnectionUtil.getDBConnection("emate1");
			// 拼写sql
			StringBuffer sql = new StringBuffer(
					"INSERT INTO t_upload_file(app_key,cust_account,fileAddress,mediaName,voice_name,content,fileFullName) "
					+ "VALUES(?,?,?,?,?,?,?)");
			// 查询获取结果
			List<JSONObject> list = coreDao.serviceJsonSql(con, sql.toString(),
					new Object[] { app_key,cust_account,fileAddress,mediaName,voice_name,content,fileFullName });
			// 获取插入的结果
			if (list != null && list.size() > 0) {
				JSONObject obj = list.get(0);
				result = obj.optInt("result");
			}

		} catch (Exception e) {
			e.printStackTrace();
			log.error("UploadFileDao.insertFileInfo 插入文件表出错：" + e);
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
	/**
	 * 更新
	 * @param coreDao
	 * @param mediaName
	 * @param voice_name
	 * @param ability_type
	 * @return
	 */
	public int updateFileInfo(CoreDao coreDao,String mediaName,String voice_name,String abilityType){
		Connection con = null;
		int result = -1;
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer("update t_upload_file set voice_name=?,abilityType=? where mediaName=?");
			List<JSONObject> list = coreDao.serviceJsonSql(con, sql.toString(),	new Object[] {voice_name,abilityType,mediaName});
			JSONObject obj = list.get(0);
			result = obj.optInt("result");

		} catch (Exception ex) {
			ex.printStackTrace();
			log.error("UploadFileDao.insertFileInfo 以mediaName="+mediaName+"时 更新文件表出错：" + ex);
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
	/**
	 * 根据嘟嘟生成唯一文件名查询整条文件信息
	 * 
	 * @param appIdentify
	 * @return
	 */
	public UploadFileModel queryFileInfoBymediaName(CoreDao coreDao, String mediaName) {
		// 数据库连接
		Connection con = null;
		// 最终返回结果
		UploadFileModel result = null;

		try {
			// 获取数据库连接
			con = DBConnectionUtil.getDBConnection("emate1");
			// 拼写sql
			StringBuffer sql = new StringBuffer("SELECT * FROM t_upload_file  where mediaName=?");
			// 查询获取结果
			List<JSONObject> list = coreDao.serviceJsonSql(con, sql.toString(), new Object[] { mediaName });
			// 类型转换
			if (list != null && list.size() > 0) {
				// 类型转换
				List<UploadFileModel> items = coreDao.covertToBean(list, UploadFileModel.class);
				result = items.get(0);
			}

		} catch (Exception ex) {
			ex.printStackTrace();
			log.error("根据嘟嘟生成唯一文件名查询整条文件信息出错：" + ex);
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
}
